package com.storlead.email.service.impl;

import com.storlead.email.service.MimeMessageHelperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Author: Timo
 * @Date: 2019/2/12 09:33
 * @Description: 类用途：
 */
@Slf4j
@Service
public class MimeMessageHelperServiceImpl implements MimeMessageHelperService {



    @Override
    public void sendEmail(String toEmail, String subject, String content, boolean html) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.qq.com");
        sender.setUsername("1002512666@qq.com");
        sender.setPassword("ddkngkrnuyusbcbj");
        sender.setDefaultEncoding("UTF-8");

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setTo("15662174827@163.com");
            helper.setText(content);
            helper.setSubject(subject);

            sender.send(message);
            log.info("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("邮件发送失败："+e.toString()+e.getMessage(),e);
        }

    }
}
